package oct2013;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class Anagrams {
    public ArrayList<String> anagrams(String[] strs) {
        int n = strs.length;
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        boolean[] flag = new boolean[n];
        for (int i = 0; i < n; ++i) {
            char[] ch = strs[i].toCharArray();
            Arrays.sort(ch);
            String tmp = new String(ch);
            Integer idx = map.get(tmp);
            if (idx == null) {
                map.put(tmp, i);
            } else {
                flag[idx] = true;
                flag[i] = true;
            }
        }
        ArrayList<String> res = new ArrayList<String>();
        for (int i = 0; i < n; ++i) {
            if (flag[i])
                res.add(strs[i]);
        }
        return res;
    }
}
